using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.Logic;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public partial class LevelServiceCustomConfig : SerializeCustomConfig
    {
    }

    public partial class EditorLevelServiceConfig : LevelServiceCustomConfig
    {
        public int id;
        public EditorLevelServiceTriggerConfig trigger;
        public List<EditorLevelServiceUnitConfig> serviceUnits = new List<EditorLevelServiceUnitConfig>();
    }

    public partial class EditorLevelServiceUnitConfig : LevelServiceCustomConfig
    {
        public string name;
        public int delay;
        public bool defaultDisable;
        public bool saveDelay;
        public EditorLevelServiceBreakConfig serviceBreak;
        public EditorLevelServiceConditionConfig condition;
        public List<EditorLevelServiceExecutionConfig> preExecution = new List<EditorLevelServiceExecutionConfig>();
        public List<EditorLevelServiceExecutionConfig> execution = new List<EditorLevelServiceExecutionConfig>();
        public List<EditorLevelServiceExecutionConfig> vetoExecution = new List<EditorLevelServiceExecutionConfig>();

        public string prefabKey;

        protected override void OnDeSerialize(ByteBuf _buf)
        {
            base.OnDeSerialize(_buf);
            if (!string.IsNullOrEmpty(this.prefabKey))
            {
                var rPrefabSource = LevelServiceEditorConfigFactory.GetServiceUnitPrefab(this.prefabKey);
                if (rPrefabSource != null)
                {
                    var rPrefab = rPrefabSource.prefab;
                    this.name = rPrefab.name;
                    this.delay = rPrefab.delay;
                    this.serviceBreak = rPrefab.serviceBreak;
                    this.condition = rPrefab.condition;
                    this.preExecution = rPrefab.preExecution;
                    this.execution = rPrefab.execution;
                    this.vetoExecution = rPrefab.vetoExecution;
                }
            }
        }
    }

    public partial class EditorLevelServiceConditionConfig : LevelServiceCustomConfig
    {
        public int conditionID;
    }

    public class EditorLevelServiceExecutionConfig : ISerializeByteBuf
    {
        public byte executionType;
        public LevelServiceCustomConfig customConfig;
        // public EditorLevelServiceTargetConfig target;

        public void Serialize(ByteBuf _buf)
        {
            _buf.WriteByte(this.executionType);
            _buf.SerializeObject(this.customConfig);
        }

        public void DeSerialize(ByteBuf _buf)
        {
            this.executionType = _buf.ReadByte();
            this.customConfig = LevelServiceEditorConfigFactory.CreateExecutionConfig((ServiceExecutionType)this.executionType, _buf);

            if (this.customConfig is LevelServiceExecutionCustom_Prefab rPrefabConfig && !string.IsNullOrEmpty(rPrefabConfig.key))
            {
                var rPrefabSource = LevelServiceEditorConfigFactory.GetExecutionPrefab(rPrefabConfig.key);
                rPrefabConfig.value = rPrefabSource?.executions;
            }
        }
    }

    public class EditorLevelServiceTriggerConfig : ISerializeByteBuf
    {
        public int triggerType;
        public LevelServiceCustomConfig customConfig;

        public void Serialize(ByteBuf _buf)
        {
            _buf.WriteInt(this.triggerType);
            SerializeByteBufUtil.SerializeObject(_buf, this.customConfig);
        }

        public void DeSerialize(ByteBuf _buf)
        {
            this.triggerType = _buf.ReadInt();
            this.customConfig = LevelServiceEditorConfigFactory.CreateTriggerConfig((ServiceTriggerType)this.triggerType, _buf);
        }
    }

    public partial class EditorLevelServiceBreakConfig : LevelServiceCustomConfig
    {
        public int breakType;
    }

    public partial class EditorLevelServicePrefabSource : LevelServiceCustomConfig
    {
        public string key;
        public string prefabPath;
        public EditorLevelServiceUnitConfig prefab;
    }

    public partial class EditorLevelServiceExecPrefabSource : LevelServiceCustomConfig
    {
        public string key;
        public string prefabPath;
        public List<EditorLevelServiceExecutionConfig> executions;
    }

    public partial class EditorLevelServiceDynamicConfig : LevelServiceCustomConfig
    {
        /// <summary>
        /// 0:固定id，1:使用动态str key
        /// </summary>
        public byte paramType;
        /// <summary>
        /// 固定id的值
        /// </summary>
        public int paramInt;
        /// <summary>
        /// 动态key的值
        /// </summary>
        public string paramStr;

        public static implicit operator DynCfgKey(EditorLevelServiceDynamicConfig editorCfg)
        {
            DynCfgKey key = new DynCfgKey();
            if (editorCfg == null)
            {
                return key;
            }
            key.isDynamic = editorCfg.paramType == 1;
            key.fixedKey = editorCfg.paramInt;
            key.dynamicKey = editorCfg.paramStr;
            return key;
        }
    }
}